package com.example.asus.widget1;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by asus on 2018/5/9.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    private List<Imagetext> mImagetextList;

    //用于把要展示的数据源传进来，并赋值给一个全局变量mImagetextList，后续操作都将在这个数据源的基础上进行
    public RecyclerViewAdapter(List<Imagetext>imagetextList){
        mImagetextList=imagetextList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Imagetext imagetext=mImagetextList.get(position);
        holder.imageView.setImageResource(imagetext.getImageId());
        holder.textView.setText(imagetext.getText());
    }

    @Override
    public int getItemCount() {
        return mImagetextList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;

        public ViewHolder(View view){
            super(view);

            imageView=(ImageView)view.findViewById(R.id.image1);
            textView=(TextView)view.findViewById(R.id.text1);
        }
    }
}
